﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using ARR.Entities;
using Telerik.Windows.Controls;
using System.IO;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for UnifiedImporterView.xaml
    /// </summary>
    public partial class UnifiedImporterView : UserControl
    {
        public UnifiedImporterView()
        {
            InitializeComponent();
        }

        private void ResultsGV_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            if (e.Row is GridViewRow && !(e.Row is GridViewNewRow))
            {
                ImporterResult importResult = e.DataElement as ImporterResult;

                foreach (var cell in e.Row.Cells)
                {
                    if (cell.Column.Name == "SelectionColumn")
                    {
                        CheckBox check = cell.Content as CheckBox;
                        if (check != null)
                            check.Visibility = (importResult.Workaround.HasValue && importResult.Workaround == Workaround.MatchCategory) ? Visibility.Visible : Visibility.Collapsed;
                    }

                    if (cell.Column.Name == "ButtonsColumn")
                    {
                        RadButton btnCategoryMatch = cell.Content as RadButton;
                        if (btnCategoryMatch != null)
                            btnCategoryMatch.Visibility = (importResult.Workaround.HasValue && importResult.Workaround == Workaround.MatchCategory) ? Visibility.Visible : Visibility.Collapsed;
                    }
                }
            }
        }

        private void ExportButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.DefaultExt = ".xls";
            dialog.Filter = "Excel File (.xls)|*.xls";
            if (dialog.ShowDialog() == true)
            {
                Telerik.Windows.Controls.GridViewExportOptions opt = new Telerik.Windows.Controls.GridViewExportOptions();
                opt.Format = Telerik.Windows.Controls.ExportFormat.Html;
                opt.ShowColumnHeaders = true;
                opt.ShowGroupFooters = false;

                using (Stream stream = dialog.OpenFile())
                {
                    this.ResultsGV.Export(stream, opt);
                }
            }
        }
    }
}
